/*
 * CArray or "chunk array" is an array type which automatically expands
 * to accommodate memory demands. Object space is allocated in chunk-
 * sized blocks and pointers to the stored objects are not invalidated
 * by resize operations.
 * The initial storage capacity of a CArray is 1 chunk.
 */

#ifndef CARRAY_H_
#define CARRAY_H_

#include "cbuf.h"

typedef struct
{
	Cbuf blocks;
	int chunk_size; // in # of elems
	int elem_size;  // sizeof one elem
	int count;
} CArray;

void carray_init (CArray* carray, int elem_size, int chunk_size);
void carray_cleanup (CArray* carray);

void* carray_append (CArray* carray, void* elem); // returns ptr to new elem
void* carray_elem (CArray* carray, int index);

#define CARRAY_APPEND(CARRAY,ELEM,TYPE) ((TYPE*) carray_append ((CARRAY),(ELEM)))
#define CARRAY_ELEM(CARRAY,INDEX,TYPE) ((TYPE*) carray_elem ((CARRAY),(INDEX)))

#endif
